﻿
@{
    Layout = "";
    ViewBag.MenuId = 3;
}
<div ng-controller="ShopCartCtrl">
    <div class="w_shopcar w_shopcar414">
        <div class="flayout_n_main" ng-show="CartList.length==0">
            <img src="/Content/images/empty_car.jpg" alt="购物车为空" class="flayout_n_img">
            <p class="flayout_n_emptyTxt">购物车空</p>
            <p class="flayout_n_tobuy">赶紧去商城选购商品吧！</p>
            <a ui-sref="shop.index" class="flayout_n_tobuyBtn">马上选购</a>
        </div>
        <div class="w_shopcar_pershop" ng-show="CartList.length>0">
            <p ng-init="myclassAll='w_shopcar_check'" ng-class="myclassAll" ng-click="CheckAllCart()">
                <input type="radio" />
                <label></label>
            </p>
            <div class="w_scPershop_ti">
                <span>{{app.name}}</span>
            </div>
            <div class="w_odt_procon" ng-repeat="cart in CartList">
                <p ng-init="cart.myclass='w_shopcar_check'" ng-class="cart.myclass" ng-click="CheckCart(cart)">
                    <input type="radio" />
                    <label></label>
                </p>
                <div class="w_odtpro_conleft"><img ng-src="{{ImgHostUrl+cart.ImgUrl}}" /></div>
                <div class="w_odtpro_conright">
                    <div class="w_odtpro_name" ui-sref="shop.productdetail({sku:cart.ProductSku,activityId:0})">{{cart.ProductName}}</div>
                    <p class="w_jfdhxq_topPost" style="margin-top: 5px">{{cart.SpecNames}}</p>
                    <div class="w_shopcar_more" style="margin-top: 5px">
                        <font color="#eb272a"><small>￥</small>{{cart.Price}}</font>
                        <img src="/Content/images/w_17.png" class="w_shopcar_del" ng-click="DelCart($index,cart)" />
                        <p class="w_shopcar_num">
                            <span ng-click="reduce_one(cart)"></span>
                            <input type="text" ng-model="cart.Num" readonly="readonly" />
                            <label ng-click="plus_one(cart)"></label>
                        </p>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!--合计-->
    <div class="w_shopcar_fixed w_spCart_fix414">
        <div class="w_shopcar_sum">
            <p ng-init="myclassAll='w_shopcar_check'" ng-class="myclassAll" ng-click="CheckAllCart()">
                <input type="radio" />
                <label></label>&nbsp;全选
            </p>
            <div class="w_shopcar_sunNum">合计：<font color="#ed3b3e">￥{{TotalMoney| number:2}}</font></div>
            <a href="" class="w_shopcar_goSum" ng-click="Buy()">去结算({{CheckedCartList.length}})</a>
        </div>
    </div>
    @Html.Partial("_homefoot")
</div>
<script type="text/javascript">
    App.controller('ShopCartCtrl', [
            '$scope', '$http', '$rootScope', '$stateParams', '$state',
        function ($scope, $http, $rootScope, $stateParams, $state) {
            $scope.TotalMoney = 0;
            $scope.CheckedCartList = [];
            $http.post("/Product/ShopCartList").then(function (returnData) {
                $scope.CartList = returnData.data;
            });
            //$scope.reddit = new Reddit("/Product/ShopCartList", { Take: 5 }, function (response) { });
            //console.log($scope.reddit);
            //$scope.reddit.nextPage();

            //数量
            $scope.reduce_one = function (cart) {
                if (cart.Num > 1) {
                    cart.Num = cart.Num - 1;
                    if (cart.myclass == "w_shopcar_check w_shopcar_checked") {
                        $scope.TotalMoney = $scope.TotalMoney - cart.Price;
                    }
                    layer.load(2);
                    $http.post("/Product/ChangeShopCartNum", { Id: cart.Id, Num: cart.Num }).then(function (returnData) {
                        layer.closeAll('loading');
                    });
                } else {
                    cart.Num = 1;
                }
            }
            $scope.plus_one = function (cart) {
                cart.Num = cart.Num + 1;
                if (cart.myclass == "w_shopcar_check w_shopcar_checked") {
                    $scope.TotalMoney = cart.Price + $scope.TotalMoney;
                }
                layer.load(2);
                $http.post("/Product/ChangeShopCartNum", { Id: cart.Id, Num: cart.Num }).then(function (returnData) {
                    layer.closeAll('loading');
                });
            }

            //删除
            $scope.DelCart = function (index, cart) {
                $http.post("/Product/DelCart", { Id: cart.Id }).then(function (returnData) {
                    if (returnData.data.isSuccess) {
                        $scope.CartList.splice(index, 1);
                        if (cart.myclass == "w_shopcar_check w_shopcar_checked") {
                            $scope.TotalMoney -= cart.Num * cart.Price;
                        }
                    }
                });
            }

            var tt = function () {
                $rootScope.WxShare($rootScope.pageTitle(), '优品三悦', location.href, $rootScope.LogoUrl);
            }

            setTimeout(tt, 1000)
            //选择购物车商品
            $scope.CheckCart = function (cart) {
                if (cart.myclass == "w_shopcar_check w_shopcar_checked") {
                    cart.myclass = "w_shopcar_check";
                    debugger
                    //if ($scope.CheckedCartList.indexOf(cart) > -1) {
                    $scope.CheckedCartList.splice($scope.CheckedCartList.indexOf(cart), 1);
                    $scope.TotalMoney -= cart.Num * cart.Price;
                    //}
                } else {
                    cart.myclass = "w_shopcar_check w_shopcar_checked";
                    $scope.CheckedCartList.push(cart);
                    $scope.TotalMoney += cart.Num * cart.Price;
                }
            }

            //全选购物车商品
            $scope.CheckAllCart = function () {
                $scope.TotalMoney = 0;
                if ($scope.myclassAll == "w_shopcar_check w_shopcar_checked") {
                    $scope.myclassAll = "w_shopcar_check";
                    angular.forEach($scope.CartList, function (cart) {
                        cart.myclass = "w_shopcar_check";
                    });
                    $scope.CheckedCartList = [];
                } else {
                    $scope.myclassAll = "w_shopcar_check w_shopcar_checked";
                    angular.forEach($scope.CartList, function (cart) {
                        cart.myclass = "w_shopcar_check w_shopcar_checked";
                        $scope.TotalMoney += cart.Num * cart.Price;
                    });
                    $scope.CheckedCartList = angular.copy($scope.CartList);
                }
                //console.log($scope.CheckedCartList);
            }

            //结算
            $scope.Buy = function () {
                if ($scope.CheckedCartList.length == 0) {
                    layer.msg("请选择商品");
                } else {
                    var ids = "";
                    angular.forEach($scope.CheckedCartList, function (cart) {
                        ids += cart.Id + ",";
                    });
                    $state.go("shop.orderconfirm", { cartids: ids, specId: 0, num: 0, activityId: 0 });
                }
            }

        }]);
</script>

